<template>
  <div class="pagination">
    <button @click="$emit('changePageNo', pageNo - 1)" :disabled="pageNo == 1">
      上一页
    </button>
    <button
      v-if="startNumAndEndNum.startNum > 1"
      @click="$emit('changePageNo', 1)"
      :class="{ active: pageNo == 1 }"
    >
      1
    </button>
    <button v-if="startNumAndEndNum.startNum > 2">···</button>
    <button
      v-for="(page, index) in startNumAndEndNumArray"
      :key="index"
      @click="$emit('changePageNo', parseInt(page))"
      :class="{ active: pageNo == page }"
    >
      {{ page }}
    </button>

    <button v-if="startNumAndEndNum.endNum < totalpage - 1">···</button>
    <button
      v-if="startNumAndEndNum.endNum < totalpage"
      @click="$emit('changePageNo', totalpage)"
      :class="{ active: pageNo == totalpage }"
    >
      {{ totalpage }}
    </button>
    <button
      @click="$emit('changePageNo', pageNo + 1)"
      :disabled="pageNo == totalpage"
    >
      下一页
    </button>

    <button style="margin-left: 30px">共 {{ total }} 条</button>
<!--     <div>
      {{ startNumAndEndNum.startNum }},{{ startNumAndEndNum.endNum
      }}{{ startNumAndEndNumArray }}
    </div> -->
  </div>
</template>
  
  <script>
export default {
  name: "Pagination",
  props: ["pageNo", "pageSize", "total", "continues"],
  computed: {
    totalpage() {
      return Math.ceil(this.total / this.pageSize);
    },
    /* 分页器起始与结束数字计算 */
    startNumAndEndNum() {
      let { pageNo, totalpage, continues } = this;
      let startNum = 0;
      let endNum = 0;
      /* 判断连续展示页数和总页数谁大谁小 */
      if (totalpage < continues) {
        startNum = 1;
        endNum = totalpage;
      } else {
        startNum = pageNo - parseInt(continues / 2);
        endNum = pageNo + parseInt(continues / 2);
        /* 当前页数太小或太大时，startNum和endNum会出现负数或者超出总页数的异常情况，需要进一步判断处理*/
        if (startNum < 1) {
          startNum = 1;
          endNum = continues;
        }
        if (endNum > totalpage) {
          startNum = totalpage - continues + 1;
          endNum = totalpage;
        }
      }
      return { startNum, endNum };
    },
    /* 计算出遍历连续分页时需要用到的数组 */
    startNumAndEndNumArray() {
      let str = "";
      for (
        let i = this.startNumAndEndNum.startNum;
        i <= this.startNumAndEndNum.endNum;
        i++
      ) {
        str = str + i + ":"; // 将数字拼接到字符串末尾,数字之间要加个标记
      }
      let arr = str.split(":"); // 按标记分隔，将字符串并转换成数组
      let newArray = arr.filter(
        (value) => value >= this.startNumAndEndNum.startNum
      );
      return newArray;
    },
  },
};
</script>
  
  <style lang="less" scoped>
.pagination {
  text-align: center;
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      /* cursor: not-allowed; */
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
  